package omg.loot;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.media.opengl.GL2;

public class Square {
   final protected FloatBuffer line;

   public Square(final float width, final float height) {
      final ByteBuffer bb = ByteBuffer.allocateDirect(8 * 4);
      bb.order(ByteOrder.nativeOrder());
      line = bb.asFloatBuffer();
      final float centerX = width*0.5f;
      final float centerY = height*0.5f;
      final float[] vt = new float[8];
      vt[0] = -centerX;       vt[1] = -centerY;  // 0, Top Left
      vt[2] = -centerX;       vt[3] = height-centerY;  // 1, Bottom Left
      vt[4] = width-centerX; vt[5] = height-centerY;  // 2, Bottom Right
      vt[6] = width-centerX; vt[7] = -centerY;  // 3, Top Right
      line.put(vt);
      line.position(0);
   }

   /**
    * The object own drawing function.
    * Called from the renderer to redraw this instance
    * with possible changes in values.
    * 
    * @param gl - The GL Context
    */
   public void draw(final GL2 gl) {
      //Point to our vertex buffer
      gl.glDisable(GL2.GL_TEXTURE_2D);
      gl.glVertexPointer(2, GL2.GL_FLOAT, 0, line);
      gl.glDrawArrays(GL2.GL_LINE_LOOP, 0, 4);
      gl.glEnable(GL2.GL_TEXTURE_2D);
   }
}